package com.study.meng.bigevent.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.groups.Default;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @TableName user
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    @Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "用户名只能包含字母、数字和下划线")
    private String username;

    @Pattern(regexp = "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@#$%^&+=!])(?=\\S+$).{8,}$",
            message = "密码需包含字母、数字和特殊符号(@#$%^&+=!)，且长度至少8位")
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)  // 只允许反序列化（接收），不允许序列化（返回）
    private String password;

    @NotEmpty(message = "昵称不能为空",groups = {User.RegisterGroup.class, User.UpdateGroup.class})
    @Pattern(regexp = "^[\\u4e00-\\u9fa5a-zA-Z0-9_]{2,10}$", message = "昵称只能包含中文、英文、数字和下划线，长度为2-10个字符")
    private String nickname;

    @NotEmpty(message = "邮箱不能为空", groups = {User.RegisterGroup.class, User.UpdateGroup.class})
    @Email
    private String email;

    private String userPic;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public interface UpdateGroup extends Default {}  // 用于分组校验，更新用户信息时使用
    public interface RegisterGroup extends Default{}  // 用于分组校验，注册用户时使用
}